package com.zlx.ssm.service.impl;


import com.zlx.ssm.mapper.MeunMapper;
import com.zlx.ssm.model.Meun;
import com.zlx.ssm.service.IUserService;
import com.zlx.ssm.vo.HomeInfo;
import com.zlx.ssm.vo.LogoInfo;
import com.zlx.ssm.vo.MenuInfo;
import com.zlx.ssm.vo.MenuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private MeunMapper mapper;
    @Override
    public MenuVO getJsonObj() {

        MenuVO menuVO = new MenuVO();
        //封装对象
        HomeInfo homeInfo=new HomeInfo();
        homeInfo.setTitle("首页");
        homeInfo.setHref("welcome");
        //封装对象
        LogoInfo logoInfo=new LogoInfo();
        logoInfo.setTitle("学生管理系统");
        logoInfo.setHref("");
        logoInfo.setImage("../images/logo.png");
        //封装对象 数组

        //VO对象的集合
        List<MenuInfo> menuInfoVo=new ArrayList<>();

        MenuInfo  menuInfo=new MenuInfo();

        menuInfo.setTitle("常规管理");
        menuInfo.setHref("");
        menuInfo.setTarget("_self");
        menuInfo.setIcon("fa fa-address-book");

        List<MenuInfo> munuInfo = this.getMunuInfo(0);
        menuInfo.setChild(munuInfo);
        menuInfoVo.add(menuInfo);


        menuVO.setMenuInfo(menuInfoVo);
        menuVO.setHomeInfo(homeInfo);
        menuVO.setLogoInfo(logoInfo);
        return menuVO;
    }

    public List<MenuInfo> getMunuInfo(Integer id){

        //VO对象的集合
        List<MenuInfo> menuInfoVo1=new ArrayList<>();

        List<Meun> meuns = mapper.selectById(id);

        for (Meun meun : meuns) {

            Integer meunId = meun.getMeunId();

            List<Meun> meuns1 = mapper.selectById(meunId);

            MenuInfo menuInfo=new MenuInfo();
                menuInfo.setTarget("_self");
                menuInfo.setHref(meun.getHref());
                menuInfo.setTitle(meun.getMeunName());
                menuInfo.setIcon(meun.getIcon());
            List<MenuInfo> munuInfo = this.getMunuInfo(meunId);
                menuInfo.setChild(munuInfo);

            menuInfoVo1.add(menuInfo);
        }

        return menuInfoVo1;
    }


}
